// pagesProperty/pages/oa/worklog/list.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js")
, common_Js = require("../../../../component/common.js");
import Dialog from '../../../../component/vant/dialog/dialog';
import Toast from '../../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading:true,
    logList:[],
    hasData:false,
    pageIndex: 1,
    pageSize: 10,
    submitState:false,
    tabs:[0,1],
    is_manager:0,
    isShowPopup:false,
    appraiseItem:{},
    parentId:0,
    is_test_user:0,
    currentTab:0,
    appraiseIndex:0,
    appraiseContent:'',
    userId:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },
  onShow(){
    this.data.pageIndex = 1;
    this.setData({
      logList:[],
    })
    this.onHttpInit()
  },
  handleChange({ detail }) {
    console.log('handleChange',this.data.isNewReq)
    this.data.is_manager = this.data.tabs[detail.index];    
    this.data.currentTab = detail.index;
    this.data.pageIndex = 1;
    this.setData({
      logList:[],
    })
    this.onHttpInit();
  },
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost("/estate/work/log/list", {
      pageSize:that.data.pageSize,
      pageIndex: that.data.pageIndex,
      is_manager: that.data.is_manager,
    }, function (res) {
      console.log("获取工作日志返回数据:", res)
      if(res.data.code == 0){
        var logList = res.data.data.logList;
        that.setData({
          is_test_user:res.data.data.is_test_user,
        })
        if(logList.length > 0){
          that.data.pageIndex += 1;
          for (var index in logList) {
            that.data.logList.splice(that.data.logList.length, 0, logList[index]);
          }
          that.setData({
            hasData: true,
            logList: that.data.logList
          })
        }
      }
      that.setData({
        isLoading: false,
      })
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, function (a) {
      console.log("获取工作日志返回数据 err:", a);
    });
  },
  goAddLog(){
    wx.navigateTo({
      url: './add/index',
    })
  },
  handlePop(){
    this.setData({
      isShowPopup: !this.data.isShowPopup,
    })
  },
  goAppraise(e){
    console.log('goAppraise', e)
    var item = e.currentTarget.dataset.item;
    var index = e.currentTarget.dataset.index;
    this.data.appraiseIndex = index;
    this.data.parentId = item.id;
    this.data.appraiseItem = item;
    this.setData({
      isShowPopup: !this.data.isShowPopup,
    })
  },
  goLike(e){
    console.log('goAppraise', e)
    var item = e.currentTarget.dataset.item;
    var index = e.currentTarget.dataset.index;
    this.data.appraiseIndex = index;
    this.data.parentId = item.id;
    this.onHttpSubmitLike();
  },
  goDelete(e){
    var id = e.currentTarget.dataset.item.id
    var message = "是否要删除该日志？";
    var that = this
    Dialog.confirm({
      title: '删除日志',
      message
    }).then(() => {
      that.onHttpDelete(id)
    })
    .catch(() => {
      Dialog.close();
    });
  },
  contentInput: function (t) {
    console.log("日志内容输入", t)
    this.data.appraiseContent = t.detail || "";
  },
  
  submitOp(){
    if(this.data.appraiseContent == '' || this.data.appraiseContent == undefined){
      Toast('请输入日志内容')
      return;
    }
    if(this.data.submitState == true){
      Toast('正在提交请稍等...')
      return;
    }
    this.data.submitState = true;
    this.setData({
      submitState: true,
    })
    this.onHttpSubmit();
  },  
  onHttpSubmit: function () {
    var that = this;
    fetchApi.httpPost("/estate/add/work/log", {
      content: that.data.appraiseContent,
      parent_id: that.data.parentId,
    }, function (res) {
      console.log("获取上报日志返回数据:", res)      
      if(res.data.code == 0){
        Toast.success(res.data.msg);
        let appraise = res.data.data.appraises;
        let appraiseCount = res.data.data.appraiseCount;
        // let logItem = that.data.logList[that.data.appraiseIndex];
        // console.log('logItem', logItem);
        // logItem.appraiseList = appraise;
        // console.log('logItem 添加 评论后', logItem);
        that.data.logList[that.data.appraiseIndex].appraiseList = appraise;
        that.data.logList[that.data.appraiseIndex].appraiseCount = appraiseCount;
        that.setData({
          logList: that.data.logList,
          appraiseContent:'',
        })
        that.data.appraiseIndex = 0;
      }else{
        Toast(res.data.msg);
      }
      // that.data.pageIndex = 1;
      // that.setData({
      //   logList:[]
      // })
      // that.onHttpInit();
      that.data.submitState = false;
      that.setData({
        submitState: false,
        isShowPopup: !that.data.isShowPopup,
      })

    }, function (a) {
      console.log("获取上报日志返回数据 err:", a);
    });
  },
  /**
   *  已读处理
   */
  onHttpSubmitLike: function () {
    var that = this;
    fetchApi.httpPost("/estate/work/log/read", {
      parent_id: that.data.parentId,
    }, function (res) {
      console.log("获取已读处理返回数据:", res)      
      if(res.data.code == 0){
        Toast.success(res.data.msg);
        that.data.logList[that.data.appraiseIndex].isRead = 1;
        that.setData({
          logList: that.data.logList,
        })
      }else{
        Toast(res.data.msg);
      }
    }, function (a) {
      console.log("获取已读处理返回数据 err:", a);
    });
  },
  onHttpDelete: function (log_id) {
    var that = this;
    fetchApi.httpPost("/estate/delete/work/log", {
      log_id: log_id,
    }, function (res) {
      console.log("删除日志响应:", res)      
      if(res.data.code == 0){
        Toast.success(res.data.msg);
      }else{
        Toast(res.data.msg);
      }
      that.data.pageIndex = 1;
      that.setData({
        logList:[]
      })
      that.onHttpInit();
    }, function (a) {
      console.log("删除日志响应返回数据 err:", a);
    });
  },
  onPullDownRefresh: function () {
    this.data.pageIndex = 1;
    this.setData({
      isLoading:true,
      logList:[],
    })
    this.onHttpInit();
  },
  /**
   * 页面上拉触底事件的处理函数    加载更多
   */
  onReachBottom: function () {
    this.onHttpInit();
  },
})